﻿using Newtonsoft.Json;
using System;
using System.Collections.ObjectModel;

namespace miniQQ_Template.Models
{
    internal class BaseConfig
    {
        [JsonIgnore]
        internal const string FileName = "Config.json";
        ///<summary>启用机器人的Q群</summary>
        internal ObservableCollection<string> EnabledGroups;
        ///<summary>操作员 QQ</summary>
        internal ObservableCollection<string> OperatorsQQ;
        ///<summary>激活的机器人 QQ</summary>
        internal ObservableCollection<string> QQRobots;
        internal BaseConfig()
        {
            EnabledGroups= new ObservableCollection<string>();
            OperatorsQQ= new ObservableCollection<string>();
            QQRobots= new ObservableCollection<string>();
        }

        internal void Refresh()
        {
            BaseConfig config = null;
            if (FileHelper.TryRead(FileName, out string content))
            {
                try
                {
                    config = JsonConvert.DeserializeObject<BaseConfig>(content);
                }
                catch (Exception ex)
                {
                    Api.ApiOutPutLog(ex.Message);
                }
            }
            if (config == null)
            {
                EnabledGroups.Clear();
                OperatorsQQ.Clear();
                QQRobots.Clear();
                FileHelper.TryWrite(BaseConfig.FileName, JsonConvert.SerializeObject(this));
            }
            else
            {
                if (config.EnabledGroups != null)
                {
                    EnabledGroups = config.EnabledGroups;
                }
                else
                {
                    EnabledGroups.Clear();
                }
                if (config.QQRobots != null)
                {
                    QQRobots = config.QQRobots;
                }
                else
                {
                    QQRobots.Clear();
                }
                if (config.OperatorsQQ != null)
                {
                    OperatorsQQ = config.OperatorsQQ;
                }
                else
                {
                    OperatorsQQ.Clear();
                }
            }
        }
    }
}